package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.beans.ResultVO;
import com.example.myapplication.beans.User;
import com.example.myapplication.utils.HttpRequestUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.resultset.ResultSet;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.event.notification.NotificationSlot;
import ohos.rpc.RemoteException;
import ohos.utils.net.Uri;

public class LoginAbilitySlice extends AbilitySlice {

    private DataAbilityHelper dataAbilityHelper;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_login);

        TextField tf1 = (TextField) findComponentById(ResourceTable.Id_login_name_textfield);
        TextField tf2 = (TextField) findComponentById(ResourceTable.Id_login_pwd_textfield);
        Button submitBtn = (Button) findComponentById(ResourceTable.Id_login_btn);
        Button submitBtns = (Button) findComponentById(ResourceTable.Id_login_btns);
        if (tf1 == null || tf2 == null || submitBtn == null) {
            new ToastDialog(this).setText("初始化组件失败").show();
            return;
        }

        submitBtn.setClickedListener(component -> {
            String inputUsername = tf1.getText();
            String inputPassword = tf2.getText();

            if (inputUsername == null || inputPassword == null) {
                new ToastDialog(this).setText("用户名或密码不能为空").show();
                return;
            }

            dataAbilityHelper = DataAbilityHelper.creator(this);
            Uri uri = Uri.parse("dataability:///com.example.myapplication.UserDataAbility/users1");

            DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates();
            dataAbilityPredicates.equalTo("username", inputUsername);

            ResultSet rs = null;
            try {
                String[] columns = {"username", "password"};
                rs = dataAbilityHelper.query(uri, columns, dataAbilityPredicates);

                if (rs != null && rs.goToFirstRow()) {
                    String usernameFromDb = rs.getString(0);
                    String passwordFromDb = rs.getString(1);
                    if (inputUsername.equals(usernameFromDb) && inputPassword.equals(passwordFromDb)) {
                        // 执行跳转操作
                        present(new MainAbilitySlice(), new Intent());
                        initNotification();
                        // 由于已经执行了跳转，所以这里返回，结束当前方法
                        return;
                    }
                }
                new ToastDialog(this).setText("用户名或密码错误").show();
            } catch (DataAbilityRemoteException e) {
                new ToastDialog(this).setText("查询失败，发生了远程数据异常").show();
                e.printStackTrace();
            } finally {
                if (rs != null) {
                    rs.close();
                }
            }
        });
        submitBtns.setClickedListener(component -> {
                        // 执行跳转操作
                        present(new RegisterAbilitySlice(), new Intent());


        });
    }

    private void initNotification() {

        NotificationSlot slot = new NotificationSlot("slot_001", "my_slot", NotificationSlot.LEVEL_DEFAULT);
        slot.setDescription("发布通知");
        slot.setEnableVibration(true);
        slot.setEnableLight(true);
        slot.setLedLightColor(Color.RED.getValue());
        try {
            NotificationHelper.addNotificationSlot(slot);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }

        int notificationId = 1001;
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(slot.getId());

        String title = "登录成功";
        String text = "欢迎您登录“有点is”";
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title);
        content.setText(text);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }
}
